<template>
  <div class="topbar">
    <div class="wrap tapbarwrap">
      <!-- 左侧 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" class="avatar" alt="" />
          用户名：{{userInfo.nickName}}
        </li>
        <li>我的积分：{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>

        <li class="cart-btn" v-if="isLogined" @click="cartFn">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{cartTotal}}</strong>
        </li>
        <li class="loginbtn" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeiXinLoginAPI } from "@/request/api";
export default {
  created() {
    this.$router.onReady(async () => {
      console.log("**topbar-created", this.$route.query);
      let pathcode = this.$route.query.code;
      //  微信扫码登录请求
      if (pathcode) {
        let res = await WeiXinLoginAPI({
          code: pathcode,
        });
        console.log("aaa", res);
        if (!res) return;
        if (res.code == 0) {
          // 扫码登录成功
          this.asyncShowToast({
            isShow: true,
            msg: "扫码登录成功",
            type: "success",
          });
          // 切换按钮(显示购物车按钮)
            this.changeIsLogined(true);
          //  存储token
          localStorage.setItem("x-auth-token", res["x-auth-token"]);     
          // 清除地址栏中code
          this.$router.push(this.$route.path); 
          // 获取用户信息
            this.asyncChangeUserInfo();
        } else if (res.code == 400) {
          // 二维码超时2min
          // code已经使用过了，再次发送
          // code错误
          // 1.提示二维码失效,请重新扫码
          // alert("二维码失效,请重新扫码")
          this.asyncShowToast({
            isShow: true,
            msg: "二维码失效,请重新扫码",
            type: "error",
          });
          // 2.自动打开登录模态框
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          // 提示用户绑定手机号码
          this.asyncShowToast({
            isShow: true,
            msg: "请使用手机号码再登录一次",
            type: "error",
          });
          // 自动打开登录模态框
          this.changeIsShowLoginModal(true);
          // 存储uuid
          localStorage.setItem("uuid", res.uuid);
          // 清除地址栏中code
          this.$router.push(this.$route.path);
        }
      }else{
        // 判断本地是否存在token
        console.log("进入了create的else代码部分....");
          let token = localStorage.getItem("x-auth-token");
          this.changeIsLogined(Boolean(token));
          if(token){
              // 已登录
              this.asyncChangeUserInfo();
          }else{
            // 未登录
            this.initUserInfo();
          }
      }
    });
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo:state=>state.userInfo.userInfo,
      cartTotal:state=>state.userInfo.cartTotal
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo:"userInfo/initUserInfo"
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      asyncChangeUserInfo:"userInfo/asyncChangeUserInfo",
    }),
    cartFn(){
      this.$router.push({path:"/user"})
    }
  },
  // watch:{
  //   "$route.path":{
  //     handler(newVal,oldVal){
  //       console.log(newVal,oldVal);
  //       // 判断本地是否存在token
  //       // if(token){
  //       //   this.changeIsLogined(true);
  //       // }else{
  //       //   this.changeIsLogined(false);
  //       // }
  //       let token = localStorage.getItem("x-auth-token");
  //       this.changeIsLogined(Boolean(token))
  //     }
  //   }
  // }
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fff;
  .tapbarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        text-align: center;
        &.loginbtn {
          background: #0a328e;
          width: 124px;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
        .avatar {
          width: 23px;
          height: 22px;
          border-radius: 50%;
          vertical-align: middle;
        }
      }
    }
  }
}
</style>